<?php
namespace app\api\model;

use app\model\Base;

class Product extends Base
{
    public function getCoverAttr($value)
    {
        return $this::getNewUrl() . $value;
    }

    public function getTitleAttr($value, $data)
    {
        return $data['name'];
    }

    public function getAliasAttr($value, $data)
    {
        return 'goods.detail';
    }

    public function getParamsAttr($value, $data)
    {
        return ['id'=>$data['id']];
    }

    public function getAmountAttr($value, $data)
    {
        return $data['sale_amount'];
    }

    public function getMarketAmountAttr($value, $data)
    {
        return $data['sale_amount'];
    }

    public function getCoverFormatAttr($value, $data)
    {
        return $this::getOssUrl() . $data['cover'];
    }

    public function getImagesFormatAttr($value, $data)
    {
        $arr = [];
        if($data['images'] != 'null'){
            $images = json_decode($data['images'], true);
            foreach ($images as $image){
                $arr[] = $this::getOssUrl() . $image;
            }
        }
        return $arr;
    }

    public function getContentAttr($value)
    {
        $str = str_replace('/uploads', $this::getOssUrl() . '/uploads', $value);
        return $str;
    }

    public function getContentFormatAttr($value, $data)
    {
        $arr = [];
        if(!is_null($data['content'])){
            if($data['content'] != 'null'){
                $images = json_decode($data['content'], true);
                foreach ($images as $image){
                    $arr[] = $this::getOssUrl() . $image;
                }
            }
        }
        return $arr;
    }

    public function getUnitFormatAttr($value, $data)
    {
        $str = "";
        switch ($data['unit']){
            case 'weight':
                $str = "斤";
                break;
            case 'number':
                $str = "个";
                break;
            case 'bag':
                $str = "袋";
                break;
            case 'casket':
                $str = "盒";
                break;
            case 'box':
                $str = "箱";
                break;
            case 'person':
                $str = "人";
                break;
        }
        return $str;
    }

    public function skuPrice()
    {
//        return $this->hasOne(ProductSku::class, 'good_id', 'id');
        return $this->hasMany(ProductSku::class, 'good_id', 'id');
    }

    //获取推荐商品
    public function getRecProduct($limit = 6)
    {
        return $this->field('state, show, nav_id, cover, id, name')->with(['sku_price'])
//            ->where('state', 'success')
            ->where('show', 'show')
            ->where('state', '<>', 'error')
//            ->where('nav_id', 3)
            ->where('merchant_id', 1)
            ->orderRaw("rand() , id DESC")
//            ->append(['sku_price'])
            ->limit($limit)
            ->select();
    }

    //获取推荐商品
    public function getRecProductByMerchant($limit = 6, $merchant_id = 1)
    {
        return $this->field('state, show, nav_id, cover, id, name')->with(['sku_price'])
//            ->where('state', 'success')
            ->where('show', 'show')
            ->where('state', '<>', 'error')
//            ->where('nav_id', 3)
            ->where('merchant_id', $merchant_id)
            ->orderRaw("rand() , id DESC")
//            ->append(['sku_price'])
            ->limit($limit)
            ->select();
    }

    public function getIndexRecProduct($limit = 5)
    {
        return $this->field('state, show, nav_id, cover, id, name, sale_amount')
//            ->with(['sku_price'])
//            ->where('state', 'success')
            ->where('show', 'show')
            ->where('state', '<>', 'error')
//            ->where('nav_id', 3)
            ->where('merchant_id', 1)
            ->orderRaw("rand() , id DESC")
            ->append(['title', 'alias', 'params', 'amount', 'market_amount'])
            ->limit($limit)
            ->select();

//        \app\api\model\Product::with(['sku_price'])
//            ->where('state', 'success')
//            ->where('show', 'show')
//            ->where('nav_id', 3)
//            ->limit('5')
//            ->select()
    }

    public function getProductByCategory($menuId, $limit = 5)
    {
        return $this->field('state, show, nav_id, cover, id, name')->with(['sku_price'])
            ->where('show', 'show')
            ->where('state', '<>', 'error')
            ->where('menu_id', $menuId)
            ->limit($limit)
            ->select();

//        \app\api\model\Product::with(['sku_price'])
//            ->where('state', 'success')
//            ->where('show', 'show')
//            ->where('nav_id', 3)
//            ->limit('5')
//            ->select()
    }
}